﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.In;
using LPV_POS.Baocao;
using LPV_POS.Baocao.Ellic;
using CrystalDecisions.CrystalReports.Engine;
using LPV_POS.Baocao.Bixolon;
using System.Resources;
using System.Globalization;


namespace LPV_POS.mh_pos
{
    public partial class mh_banhang3_ChuyenBan : DevExpress.XtraEditors.XtraForm
    {
        CultureInfo culture;
        //Boolean bInComPort = false;
        string DM_BAN_MA = "";
        string DM_BAN_MA2 = "";
        string DM_KHUVUC_MA = "";
        string sTuBan_Ten="";
        string sDenBan_Ten = "";
        DateTime timer1;

        #region "Muti language"
        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang3_ChuyenBan).Assembly);
                bton_thuchien_1.Text = rm.GetString("dongy", culture);
                bton_thoat_1.Text = rm.GetString("thoat", culture);
                lbel_tencongty.Text = rm.GetString("chuyenban", culture);
                label1.Text = rm.GetString("khuvuc", culture);
                COL_NAME.Caption = rm.GetString("ban", culture);
                //col_OBJNAME1.Caption = rm.GetString("mon", culture);
                //col_SO_LUONG1.Caption = rm.GetString("soluong", culture);
                //col_OBJNAME2.Caption = rm.GetString("mon", culture);
                //col_SO_LUONG2.Caption = rm.GetString("soluong", culture);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion


        public mh_banhang3_ChuyenBan(string _DM_BAN_MA, DateTime _timer1)
        {
            timer1 = _timer1;
            bLoadForm = false;
            InitializeComponent();
            DM_BAN_MA = _DM_BAN_MA;
            
            string sSQL = "SELECT OBJID, OBJNAME, GHICHU FROM DM_KHUVUC";
            thuvien othuvien = new thuvien();
            DataTable tbMain = othuvien.SP_SelectDataTable("sp_executesql", new string[] { "@sSQL" }, new object[] { sSQL });
            cboKhuVuc.Datasource = tbMain;
            othuvien.f_showdataoncombox(cboKhuVuc, tbMain, "OBJID", "OBJNAME");

            // -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -==
            // -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -==
            sSQL = "SELECT MAKHUVUC, OBJNAME FROM DM_BAN WHERE OBJID LIKE '" + DM_BAN_MA + "'";
            DataTable TB1 = othuvien.SP_SelectDataTable("sp_executesql", new string[] { "@Ssql" }, new object[] { sSQL });
            if (TB1.Rows.Count > 0)
            {
                DM_KHUVUC_MA = TB1.Rows[0]["MAKHUVUC"] + "";
                lbel_tencongty.Text = lbel_tencongty.Text + " (" + TB1.Rows[0]["OBJNAME"] + ")";
                sTuBan_Ten = TB1.Rows[0]["OBJNAME"] + "";
            }
            bLoadForm = true;
        }
        Boolean bLoadForm = false;
        private void mh_banhang3_ChuyenBan_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            string str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);

            //this.cboKhuVuc_SelectedIndexChanged(null, null);
            cboKhuVuc._SetselectedID(DM_KHUVUC_MA);
            this.gridView1_FocusedRowChanged(null, null);
        }

        private void cboKhuVuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (bLoadForm)
            {
                DM_KHUVUC_MA = cboKhuVuc._GetselectedID();
                string sSQL = "SELECT STT = (ROW_NUMBER() OVER (ORDER BY CAST(OBJID AS INT)))%2, * "+
                              "FROM DM_BAN "+
                              "WHERE MAKHUVUC LIKE '" + DM_KHUVUC_MA + "' "+
                              "     AND TRANGTHAI <> 1 ";
                thuvien othuvien = new thuvien();
                DataTable tbDetail = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
                gridControl1.DataSource = tbDetail;
                this.gridView1_FocusedRowChanged(null, null);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Dispose();
        }

        private void btnThucHien_Click(object sender, EventArgs e)
        {
            if (DM_BAN_MA2 != "")
            {
                int irt = new thuvien().SP_Update("sp_mh_banhang",
                                                    new string[] { "@flag", "@DM_BAN_MA", "@DM_BAN_MA2" },
                                                    new object[] { 12, DM_BAN_MA, DM_BAN_MA2 });
                if (irt == 1)
                {
                    #region "   bo"
                    //string Ngay = "* NGAY: " + timer1.ToString("dd-MM-yyyy HH:mm") + "*";

                    //string str = sTuBan_Ten + " chuyển sang " + sDenBan_Ten;
                    ////print_usb BEP
                    //thuvien tv = new thuvien();
                    //DataTable tb = tv.SP_SelectDataTable("sp_DM_MAYIN_BEP", null, null);
                    //DataView dv = tb.DefaultView;
                    //dv.Sort = "COMNAME";
                    //int vitri = dv.Find(new object[] { "USB" });
                    //int vitri_bixolon = dv.Find(new object[] { "BIXOLON" });
                    //if (tb.Rows.Count > 0)
                    //{
                    //    if (tb.Rows[0]["IN_KHI_TACHGOP_BAN"] + "" == "True")
                    //    {
                    //        if (vitri > -1 || vitri_bixolon > -1)
                    //        //if (!bInComPort)
                    //        {
                    //            #region "In bill tieng viet"
                    //            // in chuyen ban
                    //            ReportDocument rpt;
                    //            if (vitri > -1)
                    //                rpt = new bc_BaoCao_Rong();
                    //            else
                    //                rpt = new bc_BaoCao_Rong_Bixolon();

                    //            rpt.DataDefinition.FormulaFields["formula1"].Text = "'" + Ngay + "'";
                    //            rpt.DataDefinition.FormulaFields["formula2"].Text = "'" + str + "'";
                    //            rpt.PrintToPrinter(0, true, 1, 100);
                    //            #endregion
                    //        }
                    //        else
                    //        {
                    //            #region "   PrintCOMs oPrintCOM = new PrintCOMs(PrintCOMs.Print_Type.Print_KitChen);"
                    //            PrintCOMs oPrintCOM = new PrintCOMs(PrintCOMs.Print_Type.Print_KitChen);
                    //            SerialPort_LPV[] SerialPort_LPVs = oPrintCOM.Print_Start();
                    //            if (SerialPort_LPVs != null)
                    //            {
                    //                string _sPortname = ";";
                    //                foreach (SerialPort_LPV oSerialPort_LPV in Variable.gsrlCOMBillport)
                    //                {
                    //                    if (oSerialPort_LPV != null && _sPortname.IndexOf(";" + oSerialPort_LPV.PortName + ";") < 0)//&& oSerialPort_LPV.IsOpen)
                    //                    {
                    //                        if (!oSerialPort_LPV.IsOpen)
                    //                            oSerialPort_LPV.Open();
                    //                        //tbBH_PHIEU_CT_TMP.Select("(OBJID IS NULL OR OBJID = -1) AND DM_MAYIN_BEP_MA = '" + oSerialPort_LPV.OBJID + "'")

                    //                        oPrintCOM.Print_Content(oSerialPort_LPV, PrintCOMs.aligns.Align_Left, PrintCOMs.Fonts.Font_Double_Height, str, "", "", "");

                    //                        oPrintCOM.Print_Content(oSerialPort_LPV, PrintCOMs.aligns.Align_Center, PrintCOMs.Fonts.Font_Normal, Ngay, "", "", "");

                    //                        oPrintCOM.Print_Bottom(oSerialPort_LPV);

                    //                        if (oSerialPort_LPV.IsOpen)
                    //                            oSerialPort_LPV.Close();
                    //                        _sPortname = _sPortname + ";" + oSerialPort_LPV.PortName + ";";
                    //                    }
                    //                }
                    //            }
                    //            #endregion
                    //        }
                    //    }
                    //}
                    #endregion
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            else
                new mh_thongbao("Chọn bàn cần chuyển", 2).ShowDialog();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (bLoadForm)
            {
                
                    Object drv = gridView1.GetDataRow(gridView1.FocusedRowHandle);
                    if (drv != null)
                    {
                        string o2 = drv.ToString();
                        if (o2 == "System.Data.DataRow")
                        {
                            DM_BAN_MA2 = ((DataRow)drv)["OBJID"] + "";
                            sDenBan_Ten = ((DataRow)drv)["OBJNAME"] + "";
                        }
                        else if (o2 == "System.Data.DataRowView")
                        {
                            DM_BAN_MA2 = ((DataRowView)drv).Row["OBJID"] + "";
                            sDenBan_Ten = ((DataRowView)drv).Row["OBJNAME"] + "";
                        }
                    }
            }
        }
    }
}